package edu.ccut.saturn.manager.bundles.core;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.ccut.saturn.component.ComponentManager;
import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IComponentInfo;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.IParameterInfo;
import edu.ccut.saturn.component.IValidateInfo;
import edu.ccut.saturn.component.IValidateUseInfo;
import edu.ccut.saturn.component.ValidateManager;
import edu.ccut.saturn.component.impl.SaturnLogicComponentInfo;
import edu.ccut.saturn.component.impl.SaturnValidateInfo;
import edu.ccut.saturn.component.impl.SaturnValidateUseInfo;
import edu.ccut.saturn.component.xml.XMLHelper;
import edu.ccut.saturn.manager.bundles.core.Activator;


public class AddParamValidateInfo extends DefaultComponent {
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {

		String fullKey = (String) parameterContext.getParameterValue(0);
		String nodeId = (String) parameterContext.getParameterValue(1);
		String paramId = (String) parameterContext.getParameterValue(2);
		String validateInfoFullkey = (String) parameterContext
				.getParameterValue(3);

		List<String> eventList = new ArrayList<String>();
		
		if ((String) parameterContext.getParameterValue(4) != null) {
			eventList = Arrays.asList(((String) parameterContext
					.getParameterValue(4)).split(","));
		}
		
		List<String> argList = new ArrayList<String>();
		if ((String) parameterContext.getParameterValue(5) != null) {
			argList = Arrays.asList(((String) parameterContext
					.getParameterValue(5)).split(","));
		}
		
		String validateName = (String) parameterContext.getParameterValue(6);
		String description = (String) parameterContext.getParameterValue(7);	
		String validateType = (String) parameterContext.getParameterValue(8);				
		Map<String, String> errorMapInfos = this.getErrorMapInfos(parameterContext.getParameterValue(9));

	    IComponentInfo componentInfo = ComponentManager.getInstance()
				.getComponentInfo(fullKey);
		List<IParameterInfo> paramInfos = ((SaturnLogicComponentInfo) componentInfo)
				.getNodeInfo(nodeId).getParameterInfos();
		IParameterInfo paramInfo = paramInfos
				.get(Integer.parseInt(paramId) - 1);		
		IValidateUseInfo validateUses = paramInfo.getValidateUses();
		
		IValidateInfo validateInfoTemplate = ValidateManager
		.getInstance().getValidateInfo(validateInfoFullkey);
		
		IValidateInfo validateInfo = new SaturnValidateInfo(validateInfoTemplate);
		
		if (validateUses == null) {
			List<IValidateInfo> validateInfoList = new ArrayList<IValidateInfo>();
			
			validateInfo.setName(validateName);
			validateInfo.setEventInfos(eventList);
			validateInfo.setArgs(argList);
			validateInfo.setKey(validateInfoFullkey);
			validateInfo.setDescription(description);
			validateInfo.setValidateType(validateType);
			validateInfo.setErrorInfos(errorMapInfos);
			validateInfo.setJSMethodName(validateInfoTemplate.getJSMethodName());
			validateInfo.setCustemJSMethodInfo(validateInfoTemplate.getCustemJSMethodInfo());
			
			validateInfoList.add(validateInfo);
			
			IValidateUseInfo validateUseInfo = new SaturnValidateUseInfo(
					paramInfo.getValueKey(),
					paramInfo.getClassType().getName(), true, validateInfoList);			
			paramInfo.setValidateUses(validateUseInfo);
			
		} else {			
			validateInfo.setName(validateName);
			validateInfo.setEventInfos(eventList);
			validateInfo.setArgs(argList);
			validateInfo.setKey(validateInfoFullkey);
			validateInfo.setDescription(description);
			validateInfo.setValidateType(validateType);
			validateInfo.setErrorInfos(errorMapInfos);
			validateInfo.setJSMethodName(validateInfoTemplate.getJSMethodName());
			validateInfo.setCustemJSMethodInfo(validateInfoTemplate.getCustemJSMethodInfo());
			
			validateUses.getValidateInfos().add(validateInfo);
		}
        
		this.createPachFileByComponentInfo(fullKey, componentInfo);		
	}

	private void createPachFileByComponentInfo(String fullKey, IComponentInfo componentInfo) throws IOException {
		// 将componentInfo生成文件
		String dir = Activator.getServerLocation() + File.separator + "patch"
				+ File.separator;
		// "c:" + File.separator + "patch" + File.separator;
		String directPath = (fullKey.substring(0, fullKey.lastIndexOf("/")))
				.replaceAll("/", File.separator + File.separator);
		String directPathTransformed = (dir + directPath).replaceAll("::", "")
				+ File.separator;
		String componentFileName = (fullKey.substring(fullKey.lastIndexOf("/")))
				.replace("/", "");

		XMLHelper.writeFile(directPathTransformed, componentFileName,
				componentInfo.toXmlStr());
	}

	private Map<String, String> getErrorMapInfos(Object errorMapStr) {
		
		Map<String, String> errorMapInfos = new HashMap<String, String>(); 
		String[] errorMapArr = ((String) errorMapStr).split(",");
		    
		for(int i = 0; i < errorMapArr.length; i ++) {			
			String str = errorMapArr[i];
			String key = str.substring(0,str.indexOf(":"));			
			String value = str.substring(str.indexOf(":")+1);					
			errorMapInfos.put(key, value);
		}		
		return errorMapInfos;
	}
}